package com.rbac.api.mybatis.sqlProvider;


import com.rbac.api.constant.AdminConstant;
import com.rbac.api.controller.request.PostUsersListRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

public class AdminUsersSqlProvider {



    ///获取管理员列表数据条数
    public String getUsersListCountSelective(@Param("paramRequest") PostUsersListRequest usersListRequest){
        ///基础SQL
        SQL sql = new SQL();
        sql.SELECT("count(1)");
        sql.FROM("admin_users");

        ///组装筛选条件
        SQL finalSQL = AssemblyFilterCondition(usersListRequest,sql);

        ///
        return finalSQL.toString();
    }
    ///获取管理员列表数据list
    public String getUsersListDataSelective(
            @Param("paramRequest") PostUsersListRequest usersListRequest,
            @Param("StartRow") Integer StartRow,
            @Param("pageSize") Integer pageSize
    ){
        ///基础SQL
        SQL sql = new SQL();
        sql.SELECT("id,user_no,user_account,user_name,user_status");
        sql.FROM("admin_users");

        ///组装筛选条件
        SQL finalSQL = AssemblyFilterCondition(usersListRequest,sql);

        ///
        return finalSQL.toString() + " order by user_status desc limit #{StartRow,jdbcType=INTEGER} , #{pageSize,jdbcType=INTEGER}";
    }


    ///组装获取管理员列表筛选条件
    private SQL AssemblyFilterCondition(PostUsersListRequest po,SQL sq){
        ///不包含超级管理员
        sq.WHERE("`user_account` != '"+ AdminConstant.SUPER_ADMINISTRATOR_ACCOUNT_VALUE+"'");

        ///拼接筛选条件
        if(StringUtils.isNotEmpty(po.getUserAccount())){///管理员账号
            sq.WHERE("`user_account` = #{paramRequest.userAccount,jdbcType=VARCHAR}");
        }

        ///
        return sq;
    }




    ///角色操作获取可操作的且状态为有效的管理员list
    ///不包括超级管理员
    public String actionUsersListSelective(){
        ///基础SQL
        SQL sql = new SQL();
        sql.SELECT("user_no,user_name");
        sql.FROM("admin_users");

        ///筛选条件
        sql.WHERE("`user_status` = '2' ");
        sql.WHERE("`user_account` != '"+ AdminConstant.SUPER_ADMINISTRATOR_ACCOUNT_VALUE +"' ");

        ///
        return sql.toString();
    }







}